let oldversion = "";
let newversion = ""
let isCancel = true;
let appversion = "";
let downloadurl = "";

function update(url, index) {
	console.log('进入版本更新');
	uni.setStorageSync('isupapp', false);
	var _this = this;
	// 获取 manifest.json 的信息
	plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
		//  获取 app的version
		let appversion = wgtinfo.version;
		uni.request({
			url: url + '/index/getSystemVersion', //请求接口
			method: 'post',
			data: {
				now_version: appversion,
				type: index
			},
			success: result => {
				console.log('客户端类型', appversion, index);
				if (result.data.code == 1) {
					console.log('开始判断');
					if (appversion == result.data.data.newversion) {
						console.log('版本号相同', appversion, result.data.data.newversion);
					} else {
						console.log('版本号不一样', appversion, result.data.data.newversion);
						if (result.data.data.enforce == '0') {
							isCancel = true;
							showpop(isCancel, result);
						} else {
							isCancel = false;
							showpop(isCancel, result);
							uni.setStorageSync('isupapp', true);
						}
					}
				}
			},
			fail: res => {
				console.log(res);
				//失败时候回调
			}
		})
		// // 存缓存 版本号
		// try {
		// 	uni.setStorageSync('appversion', appversion);
		// } catch (e) {}
		console.log("appversion:" + appversion);
	});
}


function showpop(type, result) {
	uni.showModal({
		title: '更新',
		content: result.data.data.content,
		showCancel: type,
		success: (res) => {
			if (res.confirm) {
				console.log('用户点击确定');
				downloadurl = result.data.data.downloadurl
				isnetwork();
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}

function isnetwork() {
	//判断是否wifi下下载更新
	if (plus.networkinfo.getCurrentType() != 3) {
		uni.showModal({
			title: '发现新版本',
			content: '当前网络为非WIFI连接，确定下载？',
			success: function(res) {
				if (res.confirm) {
					doUpData()
				} else if (res.cancel) {}
			}
		});
	} else {
		uni.showModal({
			title: '发现新版本',
			content: '确定下载？',
			success: function(res) {
				if (res.confirm) {
					doUpData()
				} else if (res.cancel) {

				}
			}
		});
	}
}


function doUpData() {
	var that = this;
	var url = downloadurl
	plus.nativeUI.showWaiting("下载更新文件...");
	const downloadTask = uni.downloadFile({
		url: url, //
		success: (res) => {
			if (res.statusCode === 200) {
				installWgt(res.tempFilePath)
			}else{
				plus.nativeUI.alert("下载失败！");
			}
		},
		fail: (err) => {
			plus.nativeUI.alert("下载失败！");
		}
	});
	//监听下载速度
	downloadTask.onProgressUpdate((res) => {
		// plus.nativeUI.showWaiting("下载更新" + res.progress + "%");
		// console.log('已下载' + res.progress + '%');
	});
}

function installWgt(path) {
	plus.nativeUI.showWaiting("安装更新文件...");
	plus.runtime.install(path, {
		force: true
	}, function() {
		plus.nativeUI.closeWaiting();
		plus.nativeUI.alert("应用资源更新完成！", function() {
			//更新完成，重启app
			plus.runtime.restart();
		});
	}, function(e) {
		plus.nativeUI.closeWaiting();
		plus.nativeUI.alert("安装更新文件失败[" + e.code + "]：" + e.message);
	});
}
export default {
	update,
	doUpData
};
